package cn.itlaobing.dao;
import cn.itlaobing.entity.StationEntity;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
/**
 * Created by admin on 2017/11/9.
 */
public class StationDao {
    private DBHelper<StationEntity> db=new DBHelper<StationEntity>();
    //根据出发地、目的地、车次查询火车票的价格
    public Map<String,Object> query(String startStation, String endStation, String trainNum)throws SQLException,ClassNotFoundException{
        String sql="SELECT s1.trainNum trainNum,s1.station startStation,s2.station endStation,s2.sitePrice-s1.sitePrice as sitePrice FROM station s1,station s2 " +
                "WHERE s1.trainNum=s2.trainNum AND s1.station=? AND s2.station=? AND s1.trainNum=? ";
        ResultSet rs=db.select(sql,startStation,endStation,trainNum);
        Map<String,Object> map=new HashMap<>();
        if (rs.next()){
            map.put("startStation",rs.getString("startStation"));
            map.put("endStation",rs.getString("endStation"));
            map.put("trainNum",rs.getString("trainNum"));
            map.put("sitePrice",rs.getFloat("sitePrice"));
        }
        return map;
    }
    //关闭资源
    public void close()throws  SQLException{
        db.close();
    }
}
